package com.parse.c0004.starter;

import java.io.ByteArrayOutputStream;
import java.io.InputStream;

import org.apache.commons.lang3.StringUtils;

import android.app.Activity;
import android.app.ProgressDialog;
import android.content.ContentResolver;
import android.content.Intent;
import android.net.Uri;
import android.os.Bundle;
import android.provider.MediaStore;
import android.util.Log;
import android.view.MenuItem;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.Toast;

import com.parse.PaException.PaException;
import com.parse.api.AddProduceService;
import com.parse.api.Padelegate;
import com.parse.bean.AddProduceDTO;
import com.parse.core.AddProduceServiceImpl;
import com.parse.starter.R;
import com.parse.starter.R.id;
import com.parse.starter.R.layout;
import com.parse.starter.R.string;

/**
 * 新增商品
 * @author tsai
 *
 */
public class AddProduceActivity extends Activity implements Padelegate {

	private static final String TAG = "AddProduceActivity";
	private AddProduceDTO dto;
	private AddProduceService addProduceService;
	private ProgressDialog progressDialog;
	private static final int ADD_PRODUCE_CODE = 001;

	@Override
	protected void onActivityResult(int requestCode, int resultCode, Intent data) {

		if (resultCode == RESULT_OK) {
			final ImageView view = (ImageView) this.findViewById(R.id.image);
			if (data.getData() == null) {
				view.setVisibility(View.GONE);
			}
			Uri uri = data.getData();
			Log.v(TAG, uri.toString());

			ContentResolver resolver = getContentResolver();
			try {

				dto.setPicBytes(readStream(resolver.openInputStream(uri)));

				view.setImageBitmap(MediaStore.Images.Media.getBitmap(
						this.getContentResolver(), uri));
				view.setVisibility(View.VISIBLE);

			} catch (Exception e) {
				Log.e(TAG, uri.toString());
			}
		}

		super.onActivityResult(requestCode, resultCode, data);
	}

	public static byte[] readStream(InputStream inStream) throws Exception {
		byte[] buffer = new byte[1024];
		int len = -1;
		ByteArrayOutputStream outStream = new ByteArrayOutputStream();
		while ((len = inStream.read(buffer)) != -1) {
			outStream.write(buffer, 0, len);
		}
		byte[] data = outStream.toByteArray();
		outStream.close();
		inStream.close();
		return data;
	}

	@Override
	public boolean onMenuItemSelected(int featureId, MenuItem item) {
		int itemId = item.getItemId();

		switch (itemId) {

		case android.R.id.home:

			this.finish();
			break;
		}

		return super.onMenuItemSelected(featureId, item);

	}

	@Override
	protected void onCreate(Bundle savedInstanceState) {

		super.onCreate(savedInstanceState);
		this.setContentView(R.layout.add_produce_view);
		this.dto = new AddProduceDTO();
		this.addProduceService = new AddProduceServiceImpl();

		this.progressDialog = new ProgressDialog(this);
		this.progressDialog.setCancelable(true);

		final Button pickPicBtn = (Button) this.findViewById(R.id.pickPicBtn);
		pickPicBtn.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View arg0) {
				Intent intent = new Intent();
				intent.setType("image/*");
				intent.setAction(Intent.ACTION_GET_CONTENT);
				startActivityForResult(intent, ADD_PRODUCE_CODE);
			}
		});

		this.findViewById(R.id.submit).setOnClickListener(
				new OnClickListener() {

					@Override
					public void onClick(View arg0) {

						progressDialog.setTitle(getString(R.string.loading));
						EditText name = (EditText) AddProduceActivity.this
								.findViewById(R.id.name);
						EditText price = (EditText) AddProduceActivity.this
								.findViewById(R.id.price);
						EditText describe = (EditText) AddProduceActivity.this
								.findViewById(R.id.describe);

						dto.setTitle(name.getText().toString());
						dto.setDescribe(describe.getText().toString());
						dto.setPrice(price.getText().toString());

						if (StringUtils.isBlank(dto.getTitle())) {
							showMessage("請輸入商品名稱");
						} else if (StringUtils.isBlank(dto.getPrice())) {
							showMessage("請輸入商品價錢");
						} else if (StringUtils.isBlank(dto.getDescribe())) {
							showMessage("請輸入商品特色");
						} else if (dto.getPicBytes() == null) {
							showMessage("請選擇圖片");
						} else {
							addProduceService.uploadProduce(dto,
									AddProduceActivity.this);
							progressDialog.show();
						}

					}
				});

	}

	@Override
	public void putMessage(String requestCode, Object msg, PaException e) {

		this.progressDialog.dismiss();
		if (e == null) {
			showMessage((String) msg);
			finish();
		} else {
			;
			showMessage(e.getMessage());
		}

	}

	private void showMessage(String message) {
		Toast.makeText(this, message, Toast.LENGTH_SHORT).show();
	}
}
